<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div style="padding: 5px;">
	<form class="form-horizontal">
		<div class="form-group">
			<div class="row">
				<label for="username" class="col-sm-1 control-label">用户名:</label>
				<div class="col-sm-2">
					<input type="text" class="form-control" id="username">
				</div>
				<label for="mobilePhone" class="col-sm-1 control-label">手机号码:</label>
				<div class="col-sm-2">
					<input type="text" class="form-control" id="mobilePhone">
				</div>
				<label for="status" class="col-sm-1 control-label" style="width:8%!important;">角色:</label>
				<div class="col-sm-1">
					<select id="roleId">
						<option value="">所有</option>
						<option value="0">非一般人员</option>
						<option value="1">一般人员</option>
					</select>
				</div>
				<div class="col-sm-2">
					<button type="button" class="btn btn-primary no-border btn-sm" id="searchBtn">
						<span class="fa fa-search"></span>查询
					</button>
				</div>
			</div>
		</div>
	</form>
	<div id="userGrid" class="userGrid"></div>
</div>
<script>
	$(function() {
		var cols = [{
			title : '用户名',
			name : 'username',
			width : '150px'
		}, {
			title : 'OpenID',
			name : 'openId',
			width : '250px'
		}, {
			title : '手机号码',
			name : 'mobilePhone',
			width : '150px'
		}, {
			title : '公司',
			name : 'company',
			width : '180px'
		}, {
			title : '角色',
			name : 'wxRole',
			width : '150px',
			render : function(item, name, index) {
				return item[name] && item[name].name
			}
		}, {
			title : '状态',
			name : 'isPassed',
			width : '100px',
			render : function(item, name, index) {
				switch(item[name]) {
					case 1:
						return '已审核';
					case 2:
						return '审核不通过';
					default:
						return '未审核';
				}
			}
		}, {
			title : '地址',
			name : 'address',
			width : '350px'
		}, {
			title : '创建时间',
			name : 'createTimeStr',
			width : '350px'
		}];
		var buttons = [{
			content : '<button class="btn btn-info no-border btn-sm" type="button"><span class="fa fa-search"></span>查看</button>',
			action : 'view'
		}, {
			content : '<button class="btn btn-success no-border btn-sm" type="button"><span class="fa fa-pencil-square-o"></span>&nbsp;修改</button>',
			action : 'modify'
		}, {
			content : '<button class="btn btn-danger no-border btn-sm" type="button"><span class="fa fa-trash-o"></span>&nbsp;删除</button>',
			action : 'delete'
		}, {
			content : '<button class="btn btn-primary no-border btn-sm" type="button"><span class="fa fa-pencil-square-o"></span>&nbsp;审核</button>',
			action : 'checkRole'
		}];
		$('#userGrid').grid({
			identity : 'id',
			columns : cols,
			buttons : buttons,
			url : contextPath + '/wxUser/query'
		}).on({
			'view' : function(event, data) {
				var indexs = data.data;
				var $this = $(this);
				if (indexs.length == 0) {
					$this.message({
						type : 'warning',
						content : '请选择一条记录进行操作'
					})
					return;
				}
				if (indexs.length > 1) {
					$this.message({
						type : 'warning',
						content : '只能选择一条记录进行操作'
					})
					return;
				}
				seajs.use('wxUser', function(wxUser) {
					wxUser.view(indexs[0]);
				});
			},
			'add' : function() {
				seajs.use('wxUser', function(wxUser) {
					wxUser.add();
				});
			},
			'modify' : function(event, data) {
				var indexs = data.data;
				var $this = $(this);
				if (indexs.length == 0) {
					$this.message({
						type : 'warning',
						content : '请选择一条记录进行修改'
					})
					return;
				}
				if (indexs.length > 1) {
					$this.message({
						type : 'warning',
						content : '只能选择一条记录进行修改'
					})
					return;
				}
				seajs.use('wxUser', function(wxUser) {
					wxUser.modify(indexs[0], $this);
				});
			},
			'delete' : function(event, data) {
				var indexs = data.data;
				var $this = $(this)
				if (indexs.length == 0) {
					$this.message({
						type : 'warning',
						content : '请选择要删除的记录'
					})
					return;
				}
				$this.confirm({
					content : '确定要删除所选记录吗?',
					callBack : function() {
						seajs.use('wxUser', function(wxUser) {
							wxUser.remove(indexs, $this);
						});
					}
				});
			},
			'checkRole' : function(event, data) {
				var indexs = data.data;
				var $this = $(this);
				if (indexs.length == 0) {
					$this.message({
						type : 'warning',
						content : '请选择一条记录进行审核'
					})
					return;
				}
				if (indexs.length > 1) {
					$this.message({
						type : 'warning',
						content : '只能选择一条记录进行审核'
					})
					return;
				}
				if (data.item[0].isPassed == 1 || data.item[0].isPassed == 2) {
					$this.message({
						type : 'warning',
						content : '请选择未审核的用户'
					})
					return;
				}
				seajs.use('wxUser', function(wxUser) {
					wxUser.checkRole(indexs[0], $this);
				});
			},
			'resetPassword' : function(event, data) {
				var indexs = data.data;
				var $this = $(this);
				if (indexs.length == 0) {
					$this.message({
						type : 'warning',
						content : '请选择一条记录进行操作'
					})
					return;
				}
				if (indexs.length > 1) {
					$this.message({
						type : 'warning',
						content : '只能选择一条记录进行操作'
					})
					return;
				}
				seajs.use('wxUser', function(wxUser) {
					wxUser.resetPassword(indexs[0], $this);
				});
			}
		});
		$('#searchBtn').on('click', function() {
			seajs.use('util', function(util) {
				$('#userGrid').getGrid().search(util.getFormData(pageContents.find('form')));
			});
		});
	}); 
</script>
